// 贪心算法

const ksack = (values, weights, capacity) => {
  let load = 0
  let i = 0
  let w = 0
  
  while(load < capacity && i <4) {
    if(weights[i] <= (capacity - load)) {
      w += values[i]
      load += weights[i]  
    } else {
      let r = (capacity -load) / weights[i]
      w += r * values[i]
      load += weights[i]
    }
    i++
  }
  return w   
}


const items = ["A", "B", "C", "D"]
const values = [100, 90, 19, 222]
const weights = [70, 20, 100, 212]
const capacity = 88

console.log(ksack(values, weights, capacity))
